import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class KDF4 {
	@Test
	public void myDriver() throws Exception{
		String[][] xTC, xTS;
		String vKW, vXPath, vTestData;
		//WebDriver myD = new FirefoxDriver();
		WebDriver myD = new InternetExplorerDriver();
		// Read the TC and TS
		xTC = xl.readXL("C:\\Selenium\\A16_2012\\KDFAutomation.xls", "Test Cases");
		xTS = xl.readXL("C:\\Selenium\\A16_2012\\KDFAutomation.xls", "Test Steps");
		int vRows = xTC.length;
		int vCols = xTC[0].length;
		for (int i=1; i<xTC.length ; i++) {// Go to each Row in TC
			if (xTC[i][3].equalsIgnoreCase("Y")) { //identify which TC to execute
				for (int k=1;k<xTS.length; k++){// Go to every TS 				 
					if (xTC[i][0].equalsIgnoreCase(xTS[k][1])) { // see if TCID's match
						
						vKW = xTS[k][4];
						vXPath = xTS[k][5];
						vTestData = xTS[k][6];
						String vError = "Pass";
						try {
							// Call the corresponding Keyword Function
							if (vKW.equalsIgnoreCase("enterText")) {
								KW.enterText(myD, vXPath, vTestData);
							}
							if (vKW.equalsIgnoreCase("navigateBrowser")) {
								KW.navigateBrowser(myD,vTestData);
							}
							if (vKW.equalsIgnoreCase("clickElement")) {
								KW.clickElement(myD,vXPath);
							}
							if (vKW.equalsIgnoreCase("readText")) {
								vError = KW.readText(myD,vXPath);
								System.out.println("Text here is " + vError);
							}
							if (vKW.equalsIgnoreCase("verifyText")) {
								vError = KW.verifyText(myD,vXPath, vTestData);
								System.out.println("Test Step is a " + vError);
							}
							if (vKW.equalsIgnoreCase("waitTime")) {
								KW.waitTime(5000);
							}
							if (vError.equalsIgnoreCase("Pass")) {
								System.out.println("Pass: " + xTC[i][0] + "_" + vKW);
								xTS[k][7] = "Pass";
							} else {
								System.out.println("Fail: " + xTC[i][0] + "_" + vKW);
								xTS[k][7] = "Fail";
								xTS[k][8] = "Error happened as function returned Fail";
							}
							
						} catch(Exception selError) {
							System.out.println("Fail: " + xTC[i][0] + "_" + vKW);
							xTS[k][7] = "Fail";
							System.out.println("Error happened " + selError);
							xTS[k][8] = "Error happened " + selError;
							
						}

						
					}
				}
			}
		}
		myD.quit();
		// Write back the Test Steps with the error details into an excel
		xl.writeXL("C:\\Selenium\\A16_2012\\KDFAutomation-Result.xls", "TestSteps Result", xTS);
	}

}
